﻿using AutoMapper;
using AutoMapper.Configuration.Annotations;
using KayakMes.Check.Application.Input;
using KayakMes.Check.Domain;
using KayakMes.Common;
using KayakMes.Common.Dtos;
using KayakMes.Common.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.Check.Application.Result
{
    /// <summary>
    /// 工序检验具体实现
    /// </summary>
    public class ResultServer : IResultServer
    {
        private BaseReposiotry<CheckResultModel> baseReposiotry;

        private BaseReposiotry<CheckStandardModel> _checkstand;
        private IMapper mapper;

        public ResultServer(BaseReposiotry<CheckResultModel> baseReposiotry, IMapper mapper, BaseReposiotry<CheckStandardModel> checkstand)
        {
            this.baseReposiotry = baseReposiotry;
            this.mapper = mapper;
            _checkstand = checkstand;
        }

        /// <summary>
        /// 添加检验结果（合格）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddResultOK(AddResultInput input)
        {
            var check = baseReposiotry.GetAll().First(x => x.CheckCode == input.CheckCode);

            if (check == null)
            {
                // 新增
                check = new CheckResultModel
                {
                    Id = YitIdHelper.NextId(),
                     CreateTime= DateTime.Now,
                    CheckCode = input.CheckCode,
                    GProcessId = input.GProcessId,
                    GProcessName = input.GProcessName,
                    CheckWay = input.CheckWay,
                    SampleCountSum = input.SampleCountSum,
                    CheckVersion = input.CheckVersion,
                    SampleCount = 1,
                    PassCount = 0,
                    Inspector = input.Inspector,
                    InspectDate = DateTime.Now
                };
                baseReposiotry.Add(check);

                return new ApiResult<int> { Code = ApiEnums.Success, Message = "新增检验结果成功", Data = 1 };
            }
            else
            {
                // 更新
                if (check.SampleCount + check.PassCount >= check.SampleCountSum)
                {
                    // 更新标准状态（建议带上主键）
                    var standard = _checkstand.GetAll().First(s => s.CheckCode == input.CheckCode);
                    if (standard != null)
                    {
                        standard.IsEnabled = true;
                        _checkstand.Update(standard);
                    }
                    return new ApiResult<int> { Code = ApiEnums.Success, Message = "检验完成", Data = 1 };
                }

                check.SampleCount++;
                baseReposiotry.Update(check);
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "合格数+1", Data = 1 };
            }
        }

        /// <summary>
        /// 添加检验结果（不合格）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> AddResultNO(AddResultInput input)
        {
            var check = baseReposiotry.GetAll().First(x => x.CheckCode == input.CheckCode);

            if (check == null)
            {
                // 新增
                check = new CheckResultModel
                {
                     Id=YitIdHelper.NextId(),
                      CreateTime= DateTime.Now,
                    CheckCode = input.CheckCode,
                    GProcessId = input.GProcessId,
                    GProcessName = input.GProcessName,
                    CheckWay = input.CheckWay,
                    SampleCountSum = input.SampleCountSum,
                    CheckVersion = input.CheckVersion,
                    SampleCount = 0,
                    PassCount = 1,
                    Inspector = input.Inspector,
                    InspectDate = DateTime.Now
                };
                baseReposiotry.Add(check);

                return new ApiResult<int> { Code = ApiEnums.Success, Message = "新增检验结果成功", Data = 1 };
            }
            else
            {
                // 更新
                if (check.SampleCount + check.PassCount >= check.SampleCountSum)
                {
                    // 更新标准状态
                    var standard = _checkstand.GetAll().First(s => s.CheckCode == input.CheckCode);
                    if (standard != null)
                    {
                        standard.IsEnabled = true;
                        _checkstand.Update(standard);
                    }
                    return new ApiResult<int> { Code = ApiEnums.Success, Message = "检验完成", Data = 1 };
                }

                check.PassCount++;
                baseReposiotry.Update(check);
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "不合格数+1", Data = 1 };
            }
        }
        /// <summary>
        /// 获取检验结果
        /// </summary>
        /// <param name="checkCode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<CheckResullDTOs> GetResull(string checkCode)
        {
            var check = baseReposiotry.GetAll().First(x => x.CheckCode == checkCode);
            if(check==null)
            {
                return new ApiResult<CheckResullDTOs> { Code = ApiEnums.Fail, Message = "没有该检验结果", Data = null };
            }
            var list=mapper.Map<CheckResullDTOs>(check);
     
            list.SampleCounts=check.SampleCount;
            list.PassCounts=check.PassCount;
            return new ApiResult<CheckResullDTOs> { Code = ApiEnums.Success, Message = "获取检验结果成功", Data = list };
        }
    
    }
}
